package com.chub.study.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author : chub
 * @version : V1.0.0
 * @fileName : SpringContextUtils.java
 * @description : TODO
 * @createTime : 2024-08-02 9:04
 */
@Component
public class SpringContextUtils implements ApplicationContextAware {


    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    /**
     * @description : 获取bean对象
     *
     * @param  : reqType
     * @return : T
     */
    public static <T> T getBean(Class<T> reqType) {
        return (T) applicationContext.getBean(reqType);
    }

    /**
     * @description : 获取bean对象
     *
     * @param  : reqType
     * @return : T
     */
    public static <T> T getBean(String beanName) {
        return (T) applicationContext.getBean(beanName);
    }

    /**
     * @description : 获取bean对象
     *
     * @param  : reqType
     * @return : T
     */
    public static void cleanApplicationContext () {
        applicationContext = null;
    }

}
